package com.atguigu.tingshu.payment.api;

import ch.qos.logback.core.pattern.util.RestrictedEscapeUtil;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.payment.service.WxPayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "微信支付接口")
@RestController
@RequestMapping("api/payment")
@Slf4j
public class WxPayApiController {

    @Autowired
    private WxPayService wxPayService;


    /**
     * 获取小程序拉起微信支付所需参数
     *
     * @param paymentType
     * @param orderNo
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "获取小程序拉起微信支付所需参数")
    @PostMapping("/wxPay/createJsapi/{paymentType}/{orderNo}")
    public Result<Map<String, String>> getClientPayRequestParams(@PathVariable String paymentType, @PathVariable String orderNo) {
        Map<String, String> map = wxPayService.getClientPayRequestParams(paymentType, orderNo);
        return Result.ok(map);
    }


    /**
     * 用户通过微信支付支付成功后，处理微信支付异步回调
     * （微信通知商户用户支付结果）
     * @param request
     * @return {code:"SUCCESS",message:"处理成功"}
     */
    //@PostMapping("/wxPay/notify")
    public Result wxPaySuccessNotify(HttpServletRequest request){
        Map<String, String> map = wxPayService.wxPaySuccessNotify(request);
        return Result.ok(map);
    }


    /**
     * 商户主动调用微信支付获取订单支付状态
     * @param orderNo
     * @return true：只支付  false：未支付
     */
    @GuiGuLogin
    @Operation(summary = "支付状态查询")
    @GetMapping("/wxPay/queryPayStatus/{orderNo}")
    public Result<Boolean> queryOrderPayStatus(@PathVariable String orderNo){
        Boolean isPaid = wxPayService.queryOrderPayStatus(orderNo);
        return Result.ok(isPaid);
    }

}
